package com.pds.web.testOrganizationInfo.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pds.common.utils.DateUtils;
import com.pds.common.utils.SecurityUtils;
import com.pds.web.testOrganizationInfo.domain.PdsTestOrganizationInfo;
import com.pds.web.testOrganizationInfo.domain.PdsTestOrganizationPlace;
import com.pds.web.testOrganizationInfo.mapper.PdsTestOrganizationInfoMapper;
import com.pds.web.testOrganizationInfo.mapper.PdsTestOrganizationPlaceMapper;
import com.pds.web.testOrganizationInfo.service.IPdsTestOrganizationPlaceService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class PdsTestOrganizationPlaceServiceImpl extends ServiceImpl<PdsTestOrganizationPlaceMapper, PdsTestOrganizationPlace> implements IPdsTestOrganizationPlaceService {

    @Resource
    private PdsTestOrganizationPlaceMapper pdsTestOrganizationPlaceMapper;
    @Resource
    private PdsTestOrganizationInfoMapper pdsTestOrganizationInfoMapper;

    /**
     * 新增
     *
     * @param pdsTestOrganizationPlace
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public PdsTestOrganizationPlace addPlace(PdsTestOrganizationPlace pdsTestOrganizationPlace) {

//        Long userId = SecurityUtils.getLoginUser().getUser().getUserId();
//        PdsTestOrganizationInfo pdsTestOrganizationInfo = pdsTestOrganizationInfoMapper.selectByUserId(userId);
//        pdsTestOrganizationPlace.setOrganizationId(pdsTestOrganizationInfo.getId());
//        pdsTestOrganizationPlace.setUserId(userId);

        Date date = DateUtils.parseDate(DateUtils.getTime());
        pdsTestOrganizationPlace.setCreateTime(date);

        pdsTestOrganizationPlace.setStatus("1");

        pdsTestOrganizationPlaceMapper.addPlace(pdsTestOrganizationPlace);

        return pdsTestOrganizationPlace;
    }

    /**
     * 删除 -- 根据id
     *
     * @param id
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public Boolean deletePlace(Long id) {
        return pdsTestOrganizationPlaceMapper.deletePace(id) > 0;
    }

    /**
     * 更新
     *
     * @param pdsTestOrganizationPlace
     */
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    @Override
    public Boolean updatePlace(PdsTestOrganizationPlace pdsTestOrganizationPlace) {

        Date date = DateUtils.parseDate(DateUtils.getTime());
        pdsTestOrganizationPlace.setUpdateTime(date);

        return pdsTestOrganizationPlaceMapper.updatePlace(pdsTestOrganizationPlace) > 0;
    }

    /**
     * 查询 -- 分页/条件：userId，status
     *
     * @param pdsTestOrganizationPlace
     * @return
     */
    @Override
    public List<PdsTestOrganizationPlace> selectPlaceList(PdsTestOrganizationPlace pdsTestOrganizationPlace) {
        return pdsTestOrganizationPlaceMapper.selectPlaceList(pdsTestOrganizationPlace);
    }
}
